<template>
    <div style="color: #fff;">
        <div class="registerbox">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
                <!-- 账号 -->
                <el-form-item label="" prop="username">
                    <el-input placeholder="输入手机号" type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>

                <!-- 密码 -->
                <el-form-item label="" prop="password">
                    <el-input placeholder="设置密码" type="password" v-model="ruleForm.password" autocomplete="off" ></el-input>
                </el-form-item>

                <!-- 确认密码 -->
                <el-form-item label="" prop="checkPass">
                    <el-input placeholder="确认密码" type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="submitRegister('ruleForm')">注册</el-button>
                    <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

import { register } from '@/api/user/register'
import { mapState } from 'vuex';


export default {
    name: 'Vue2Register',

    data() {
        // 账号规则函数
        var validateUser = (rule, value, callback) => {
            if (value === '') {
                return callback(new Error('账号不能为空'));
            }
            else {
                callback();
            }
        };

        // 密码规则函数
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };

        // 密码二次验证
        var validatePass2 = (rule, value, callback) => {
        if (value === '') {
            callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
            callback(new Error('两次输入密码不一致!'));
            } else {
            callback();
            }
        };
        return {
            ruleForm: {
                password: '',
                username: '',
                checkPass: '',
            },
            // 账号密码验证规则对象
            rules: {
                // 第一个参数表示必填项 如果没有符合要求就会输出message的信息
                // trigger: 'blur'表示鼠标离开后验证
                username: [
                    {min: 1 , max: 12 , message: '长度必须在6-12个字符', trigger: 'blur'},
                    { validator: validateUser, trigger: 'blur' }
                ],
                password: [
                    {min: 1 , max: 18 , message: '长度必须在6-18个字符', trigger: 'blur'},
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        };
    },

    computed: {
        ...mapState(['isChecked'])
    },

    mounted() {
        
    },

    methods: {
        submitRegister(ruleForm){
            if(this.isChecked){
                this.$refs[ruleForm].validate((valid) => {
                    if (valid) {
                        let user = this.ruleForm.username
                        let pass = this.ruleForm.password
                        // 调用aiox注册接口
                        register(user,pass).then((res)=>{
                            // console.log(res.data.status)
                            alert(res.data.message)
                            if(res.data.status == 200){
                                this.$router.replace('/user/login')
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                })
            }else{
                alert('请阅读并勾选用户协议')
            }
        }
    },
};
</script>

<style lang="scss">
@import '@/assets/css/user/register.scss'
</style>